#include <iostream>
#include <string.h>
#define int unsigned long long
using namespace std;




signed main(){
	int ans=0;
	int n,p;
	cin>>n>>p;
	bool dp1[p],dp2[p];
	memset(dp1,false,sizeof(dp1));
	memset(dp2,false,sizeof(dp2));
	int a[n+1];
	for(int i=1;i<=n;i++){
		cin>>a[i];
		a[i] %= p;
	}
	
	for(int i=1;i<=n;i++){
		dp1[0] = true;
		for(int j=0;j<p;j++){
			dp2[j] = dp1[(j+p-a[i])%p] || dp1[j];
			if(dp2[j]  &&  j > ans)
				ans = j;
		}
		memcpy(dp1,dp2,sizeof(dp1));
	}
	cout<<ans;
	return 0;
}
